<template>
    <div>
        <!--    面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
<!--        卡片区域-->
        <el-card>
            <el-row>
                <el-button type="primary" @click="addRoles">添加角色</el-button>
            </el-row>
            <el-table :data="this.rolesList" border stripe>
<!--                展开行-->
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row v-for="(item1,i) in scope.row.children" :key="item1.id" :class="['border-bottom',i===0?'border-top':'','vertical-center']">
<!--                            一级权限-->
                            <el-col :span="5">
                                    <el-tag @close="deleteRights(scope.row,item1.id)" closable>{{item1.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                            </el-col>
<!--                            二级权限和三级权限-->
                            <el-col :span="19">
                                <el-row v-for="(item2,i) in item1.children" :key="item2.id" :class="[i===0?'':'border-top','vertical-center']">
<!--                                    二级权限-->
                                    <el-col :span="6">
                                        <el-tag @close="deleteRights(scope.row,item2.id)" closable type="success">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
<!--                                    三级权限-->
                                    <el-col :span="18">
                                        <el-tag @close="deleteRights(scope.row,item3.id)" closable type="warning" v-for="(item3,i) in item2.children" :key="item3.id">
                                            {{item3.authName}}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
<!--                索引栏-->
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="300px">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteRoles(scope.row)">删除</el-button>
                        <el-button size="mini" type="warning" icon="el-icon-setting" @click="assignRoles(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>

            </el-table>
        </el-card>
<!--        分配角色权限弹出框-->
        <el-dialog title="分配权限" :visible.sync="assignRolesDialogVisible" width="50%" @close="setRightDialogClosed">
            <el-tree ref="treeRef" :data="rolesTree" :props="defaultProps" show-checkbox default-expand-all node-key="id" :default-checked-keys="defkeys">
            </el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="assignRolesDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmRoleAssignment()">确 定</el-button>
            </span>
        </el-dialog>
<!--        添加角色弹出框-->
        <el-dialog title="添加角色" :visible.sync="addRolesDiaVis" width="30%" @close="setAddRolesClosed">
            <el-form ref="addRolesForm" :rules="addRolesRules" :model="addRolesForm">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="addRolesForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="addRolesForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addRolesDiaVis = false">取 消</el-button>
                <el-button type="primary" @click="confirmAddRoles()">确 定</el-button>
            </span>
        </el-dialog>
<!--        编辑角色弹出框-->
        <el-dialog title="编辑角色" :visible.sync="editRolesDiaVis" width="30%" @close="editRolesClosed">
            <el-form ref="editRolesForm" :rules="editRolesRules" :model="editRolesForm">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="editRolesForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="editRolesForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editRolesDiaVis = false">取 消</el-button>
                <el-button type="primary" @click="confirmEditRoles()">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "roles",
        data(){
            return{
                rolesList:[],
                assignRolesDialogVisible:false,
                rolesTree:[],
                defaultProps:{
                    children:'children',
                    label:'authName'
                },
                defkeys:[],
                roleId:'',
                addRolesForm:{
                  roleName:'',
                  roleDesc:''  
                },
                addRolesRules:{
                    roleName:[{required:true,message:'请输入角色名',trigger:'blur'}],
                    roleDesc:[{required:true,message:'请输入角色描述',trigger:'blur'}]
                },
                addRolesDiaVis:false,
                editRolesForm:{
                    roleName:'',
                    roleDesc:''
                },
                editRolesRules:{
                    roleName:[{required:true,message:'请输入角色名',trigger:'blur'}],
                    roleDesc:[{required:true,message:'请输入角色描述',trigger:'blur'}]
                },
                editRolesDiaVis:false,
                id:0
            }
        },
        created() {
            this.getRolesList()
        },
        methods:{
            //获取角色列表
            getRolesList(){
                this.$http.get('roles').then((res)=>{
                    if (res.data.meta.status==200){
                        this.rolesList=res.data.data
                    }else {
                        this.$message.error('获取角色列表失败')
                    }
                })
            },
            //删除权限
            deleteRights(role,rightId){
                this.$confirm('此操作将永久删除该权限，是否继续？','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:"warning"
                }).then(()=>{
                    this.$http.delete('roles/'+role.id+'/rights/'+rightId).then((res)=>{
                        if (res.data.meta.status==200){
                            role.children=res.data.data
                        }else {
                            this.$message.error('删除权限失败')
                        }
                    })
                }).catch(()=>{
                    return this.$message.info('已取消删除')
                })
            },
            //分配角色权限
            assignRoles(role){
                this.roleId=role.id
                //获取权限树
                this.$http.get('rights/tree').then((res)=>{
                    this.rolesTree=res.data.data
                })
                this.getLeafKeys(role,this.defkeys)
                this.assignRolesDialogVisible=true
            },
            //通过递归的方式获取当前角色下的所有三级节点，并保存到defkeys数组中
            getLeafKeys(node,arr){
                if (!node.children){
                    return arr.push(node.id)
                }
                node.children.forEach((item)=>
                    this.getLeafKeys(item,arr)
                )
            },
            setRightDialogClosed(){
                this.defkeys=[]
            },
            //确认分配角色权限
            confirmRoleAssignment(){
                const idarr=[...this.$refs.treeRef.getCheckedKeys(),...this.$refs.treeRef.getHalfCheckedKeys()]
                const idStr=idarr.join(',')
                this.$http.post('roles/'+this.roleId+'/rights',{rids:idStr}).then((res)=>{
                    if (res.data.meta.status==200){
                        this.$message.success('分配权限成功')
                    }else{
                        this.$message.error('分配权限失败')
                    }
                    this.getRolesList()
                    this.assignRolesDialogVisible=false
                })
            },
            //添加角色
            addRoles(){
                this.addRolesDiaVis=true
            },
            confirmAddRoles(){
                this.$refs.addRolesForm.validate(res=>{
                    if (!res){
                        return
                    }else {
                        this.$http.post('roles',this.addRolesForm).then((res)=>{
                            if (res.data.meta.status!==201){
                                this.$message.error('添加角色失败')
                            }else{
                                this.$message.success('添加角色成功')
                                this.setAddRolesClosed()
                                this.getRolesList()
                            }
                        })
                    }
                })
            },
            setAddRolesClosed(){
                this.$refs.addRolesForm.resetFields()
                this.addRolesDiaVis=false
            },
            edit(row){
                console.log(row)
                this.editRolesForm.roleDesc=row.roleDesc
                this.editRolesForm.roleName=row.roleName
                this.id=row.id
                this.editRolesDiaVis=true
            },
            editRolesClosed(){
                this.editRolesDiaVis=false
            },
            confirmEditRoles(){
                this.$refs.editRolesForm.validate(res=>{
                    if (!res){
                        return
                    }else {
                        this.$http.put('roles/'+this.id,this.editRolesForm).then(res=>{
                            if (res.data.meta.status!==200){
                             this.$message.error('修改失败')   
                            }else{
                                this.$message.success('修改成功')
                                this.editRolesClosed()
                                this.getRolesList()
                            }
                        })
                    }
                })
            },
        //    删除角色
            deleteRoles(row){
                this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('roles/'+row.id).then(res=>{
                        if (res.data.meta.status!==200){
                            this.$message.error('删除失败')
                        }else{
                            this.$message.success('删除成功')
                            this.getRolesList()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style scoped>
.el-tag{
    margin: 7px;
}
    .border-top{
        border-top: 1px solid #eeeeee;
    }
    .border-bottom{
        border-bottom: 1px solid #eeeeee;
    }
    .vertical-center{
        display: flex;
        align-items: center;
    }
</style>
